﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KorleBu.Application_Classes;

namespace KorleBu
{
    public partial class RequestReport : System.Web.UI.Page
    {
        public String description { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            int rID;

            if (Session["RequestID"] != null)
            {
                rID = Convert.ToInt32(Session["RequestID"]);
                FillTableInfo(rID);
                BindReceipt(rID);
            }
            else
            {
                lblMasterError.Text = "Sorry, we lost track of the Requisition to report. Try logging out, logging back in and request this report again.";
            }
        }

        /// <summary>
        /// Adam Venezia 10-21-12
        /// Call proper stored procedure to get the passed in requisition's information
        /// and store to table above the items listing.
        /// </summary>
        /// <param name="rID"></param>
        private void FillTableInfo(int rID)
        {
            Presentation presentation = new Presentation();
            DataSet ds = presentation.GetRequestsByStoreID(GetStoreID());
            DataTable dt;
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                dt = ds.Tables[1];
                //fill in labels
                lblViewRequestID.Text = rID.ToString();
                lblViewDate.Text = dt.Rows[0]["RequestDate"].ToString();
                lblViewDescription.Text = dt.Rows[0]["RequestDescription"].ToString();
                //fill in description property, which value is used to create a proper page title.
                description = dt.Rows[0]["RequestDescription"].ToString();
            }
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
        }

        /// <summary>
        /// Adam Venezia 10-21-12
        /// Call proper stored procedure to get the requisition items from passed in requisition
        /// and bind to gvViewRequisitionItems.
        /// </summary>
        /// <param name="rID"></param>
        private void BindReceipt(int rID)
        {
            Presentation presentation = new Presentation();
            DataSet ds = presentation.GetRequestItemsByRequestID(rID, GetStoreID());
            DataTable dt;
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                dt = ds.Tables[1];
                gvViewRequestItems.DataSource = dt;
                gvViewRequestItems.DataBind();
            }
            else
                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
        }

        private int GetStoreID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getStoreId();
        }
    }
}